package com.yuwenchao.superafflatus.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Api(tags = "逻辑游戏测试")
@RestController
public class SuperGuessController {

	

	@ApiOperation("生成不重复的4位数")
	@GetMapping("getRandom")
	public List<String> getRandom(HttpSession session){
		   ArrayList<String> list = new ArrayList<>();
		   ArrayList<String> sys = new ArrayList<>();
		    Random random = new Random();
		    int i =0;
		    while(i<4) {
		    	int r =random.nextInt(10);
		    	if(!list.contains(r+"")) {
		    		list.add(r+"");
		    		sys.add(r+"");
		    		i++;
		    	}
		    }
		    session.setAttribute("list", list);
		    session.setAttribute("sys", sys);
		return list;
	}
	

	@ApiOperation("开始游戏")
	@PostMapping(value="startRandom",produces= {"application/text;charset=UTF-8"})
	public String startRandom(HttpSession session, Model model, @RequestParam("gamenum") String gamenum){
		String msg="";
		if(gamenum.length()==4) {
			List<String> gameList = new ArrayList<>();
			String a0 = String.valueOf(gamenum.charAt(0));
			String a1 = String.valueOf(gamenum.charAt(1));
			String a2 = String.valueOf(gamenum.charAt(2));
			String a3 = String.valueOf(gamenum.charAt(3));
			gameList.add(a0);
			gameList.add(a1);
			gameList.add(a2);
			gameList.add(a3);
			
			List<String> list =(List<String>) session.getAttribute("list");
			List<String> sys =(List<String>) session.getAttribute("sys");
			model.addAttribute("list",list);
		
			
			if(a0.equals(a1)||a0.equals(a2)||a0.equals(a3)||a1.equals(a2)||a1.equals(a3)||a1.equals(a3)) {
				msg="输入有误,不能输入重复数字";
				return msg;
			}
			
			list.retainAll(gameList);
			if(list.size()==0) {
				msg="0A0B,运气不错，没有数字相同";
				list.clear();
				for (String string : sys) {
					list.add(string);
				}	
			}else if(list.size()==1) {
				String value1 = list.toString().substring(1,2);
				int sysindex1 = sys.indexOf(value1);
				int gameListindex1 = gameList.indexOf(value1);
				if(sysindex1==gameListindex1) {
					msg="1A0B";
				}else{
					msg="0A1B";
				}
				list.clear();
				for (String string : sys) {
					list.add(string);
				}	
			}else if(list.size()==2) {
				String value1 = list.toString().substring(1,2);
				String value2 = list.toString().substring(4,5);
				int sysindex1 = sys.indexOf(value1);
				int gameListindex1 = gameList.indexOf(value1);
				int sysindex2 = sys.indexOf(value2);
				int gameListindex2 = gameList.indexOf(value2);
				if(sysindex1==gameListindex1&&sysindex2==gameListindex2) {
					msg="2A0B";
				}else if(sysindex1!=gameListindex1&&sysindex2!=gameListindex2) {
					msg="0A2B";
				}else {
					msg="1A1B";
				}
				list.clear();
				for (String string : sys) {
					list.add(string);
				}		
			}else if(list.size()==3) {
				String value1 = list.toString().substring(1,2);
				String value2 = list.toString().substring(4,5);
				String value3 = list.toString().substring(7,8);
				int sysindex1 = sys.indexOf(value1);
				int gameListindex1 = gameList.indexOf(value1);
				int sysindex2 = sys.indexOf(value2);
				int gameListindex2 = gameList.indexOf(value2);
				int sysindex3 = sys.indexOf(value3);
				int gameListindex3 = gameList.indexOf(value3);
				if(sysindex1==gameListindex1&&sysindex2==gameListindex2&&sysindex3==gameListindex3) {
					msg="3A0B";
				}else if(sysindex1!=gameListindex1&&sysindex2!=gameListindex2&&sysindex3!=gameListindex3) {
					msg="0A3B";
				}else if(sysindex1==gameListindex1&&sysindex2!=gameListindex2&&sysindex3!=gameListindex3) {
					msg="1A2B";
				}else if(sysindex1!=gameListindex1&&sysindex2==gameListindex2&&sysindex3!=gameListindex3) {
					msg="1A2B";
				}else if(sysindex1!=gameListindex1&&sysindex2!=gameListindex2&&sysindex3==gameListindex3) {
					msg="1A2B";
				}else {
					msg="2A1B";	
				}
				list.clear();
				for (String string : sys) {
					list.add(string);
				}		
			}else if(list.size()==4) {
				String value1 = list.toString().substring(1,2);
				String value2 = list.toString().substring(4,5);
				String value3 = list.toString().substring(7,8);
				String value4 = list.toString().substring(10,11);
				int sysindex1 = sys.indexOf(value1);
				int gameListindex1 = gameList.indexOf(value1);
				int sysindex2 = sys.indexOf(value2);
				int gameListindex2 = gameList.indexOf(value2);
				int sysindex3 = sys.indexOf(value3);
				int gameListindex3 = gameList.indexOf(value3);
				int sysindex4 = sys.indexOf(value4);
				int gameListindex4 = gameList.indexOf(value4);
				if(sysindex1!=gameListindex1&&sysindex2!=gameListindex2
				 &&sysindex3!=gameListindex3&&sysindex4!=gameListindex4) {
					msg="0A4B";
				}else if(sysindex1==gameListindex1&&sysindex2==gameListindex2
						 &&sysindex3==gameListindex3&&sysindex4==gameListindex4) {
					msg="恭喜你猜对了！";
				}else if(sysindex1==gameListindex1&&sysindex2==gameListindex2
						 &&sysindex3!=gameListindex3&&sysindex4!=gameListindex4) {
					msg="2A2B";
				}else if(sysindex1!=gameListindex1&&sysindex2!=gameListindex2
						 &&sysindex3==gameListindex3&&sysindex4==gameListindex4) {
					msg="2A2B";
				}else if(sysindex1!=gameListindex1&&sysindex2==gameListindex2
						 &&sysindex3!=gameListindex3&&sysindex4==gameListindex4) {
					msg="2A2B";
				}else if(sysindex1==gameListindex1&&sysindex2!=gameListindex2
						 &&sysindex3!=gameListindex3&&sysindex4==gameListindex4) {
					msg="2A2B";
				}else{
					msg="1A3B";
				}
				list.clear();
				for (String string : sys) {
					list.add(string);
				}	

			}else {
				msg="输入有误，数字必须等于4位";
				return msg;
			}
		}	
		
		       return msg;
	}
	
	
}
